#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 15;
int a[N], b[N];
int ta[N];
int op[N], x[N], y[N];
int n, k;

bool check(vector<int> &v){
	for (int i=1;i<=n;i++) {
		ta[i] = a[i];
	}
	for (int i: v) {
		int a = op[i];
		int b = x[i];
		int c = y[i];
		b+=1;
		if (a==1) {
			ta[b] = (ta[b] + c) % 10;
		} else {
			c+=1;
			swap(ta[b], ta[c]);
		}
	}
	
	for (int i=1;i<=n;i++) {
		if (ta[i] != b[i]) {
			return false;
		}
	}
	return true;
}



void solve(){
	cin >> n;
	for (int i=1;i<=n;i++) {
		char c;
		cin >> c;
		a[i] = c-'0';
	}
	for (int i=1;i<=n;i++) {
		char c;
		cin >> c;
		b[i] = c-'0';
	}
	cin >> k;
	for (int i=1;i<=k;i++) {
		cin >> op[i] >> x[i] >> y[i];
	}
	
	for (int i=0;i<(1<<k);i++) {
		vector<int> v;
		for (int j=0;j<k;j++) {
			if ((i>>j)&1) {
				// 表示下标为 j+1 的被选上了
				v.push_back(j+1);
			}
		}
		do{
			//TODO
			if (check(v)) {
				cout << "Yes" << '\n';
				return;
			}
 		}while(next_permutation(v.begin(), v.end())); // 下一个字典序
	}
	cout << "No" << '\n';
}


signed main(){
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}
